<template>
  <div class="app-container">
    <div v-if="crud.props.searchToggle">
      <el-input v-model="query.name" clearable size="small" placeholder="输入图书名称搜索" style="width: 200px;"
                class="filter-item" @keyup.enter.native="crud.toQuery"/>
      <rrOperation/>
    </div>
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
      </crudOperation>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
                 :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="封面路径" >
            <el-input v-model="form.coverPath" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="作者" prop="author">
            <el-input v-model="form.author" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="图书类型" prop="type">
            <el-select v-model="form.type" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.book_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="图书介绍" >
            <el-input v-model="form.introduce" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="图书分类名称" >
            <el-input v-model="form.classificationName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="共享url" >
            <el-input v-model="form.url" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="资源路径" >
            <el-input v-model="form.resourcePath" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="出版商" >
            <el-input v-model="form.publisher" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="是否共享" >
            <template>
              <el-radio v-model="form.share" label="0">不共享</el-radio>
              <el-radio v-model="form.share" label="1">共享</el-radio>
            </template>
          </el-form-item>
          <el-form-item label="内容" >
            <el-input v-model="form.content" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="版权" >
            <el-input v-model="form.copyright" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="页面索引" >
            <el-input v-model="form.pageIndex" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="位置" >
            <el-input v-model="form.position" style="width: 370px;"/>
          </el-form-item>

          <el-form-item label="备注">
            <el-input v-model="form.remark" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="状态">
            <el-switch
              :active-value="1"
              :inactive-value="0"
              v-model="form.status"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;"
                @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="id" label="id"/>
        <el-table-column prop="name" label="图书名称"/>
        <el-table-column prop="coverPath" label="封面路径"/>
        <el-table-column prop="author" label="作者"/>
        <el-table-column prop="type" label="图书类型">
          <template slot-scope="scope">
            {{ dict.label.book_type[scope.row.type] }}
          </template>
        </el-table-column>
        <el-table-column prop="introduce" label="图书介绍"/>
        <el-table-column prop="classificationName" label="图书分类名称"/>
        <el-table-column prop="url" label="共享url"/>
        <el-table-column prop="resourcePath" label="资源路径"/>
        <el-table-column prop="publisher" label="出版商"/>
        <el-table-column prop="share" label="是否共享">
          <template slot-scope="scope">
            <span v-if="scope.row.share === '1'">共享</span>
            <span v-else>不共享</span>
          </template>
        </el-table-column>
        <el-table-column prop="content" label="内容"/>
        <el-table-column prop="copyright" label="版权"/>
        <el-table-column prop="pageIndex" label="页面索引"/>
        <el-table-column prop="position" label="位置"/>
        <el-table-column prop="createBy" label="创建者"/>
        <el-table-column prop="updateBy" label="修改者"/>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-switch
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              changeEnabled
              @change="changeState(scope.row,scope.row.status)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','book:edit','book:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination/>
    </div>
  </div>
</template>

<script>
import crudBook from '@/api/videoAndBook/book'
import CRUD, {presenter, header, form, crud} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import {getByName} from "../../../api/videoAndBook/book";

const defaultForm = {
  id: null,
  name: null,
  coverPath: null,
  author: null,
  type: null,
  introduce: null,
  classificationName: null,
  url: null,
  resourcePath: null,
  publisher: null,
  share: null,
  content: null,
  copyright: null,
  pageIndex: null,
  position: null,
  createBy: null,
  createTime: null,
  updateBy: null,
  updateTime: null,
  status: null,
  remark: null
}
export default {
  name: 'Book',
  components: {pagination, crudOperation, rrOperation, udOperation},
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['book_type'],
  cruds() {
    return CRUD({title: '图书管理', url: 'api/book', idField: 'id', sort: 'id,desc', crudMethod: {...crudBook}})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'book:add'],
        edit: ['admin', 'book:edit'],
        del: ['admin', 'book:del']
      },
      input: '',
      rules: {
        name: [
          {required: true, message: '请输入名称', trigger: 'change'}
        ],
        coverPath: [
          {required: true, message: '请输入封面路径', trigger: 'change'}
        ],
        author: [
          {required: true, message: '请输入作者', trigger: 'change'}
        ],
        type: [
          {required: true, message: '请选择图书类型', trigger: 'change'}
        ],
        introduce: [
          {required: true, message: '请输入图书介绍', trigger: 'change'}
        ],
        classificationName: [
          {required: true, message: '请输入图书分类名称', trigger: 'change'}
        ],
        url: [
          {required: true, message: '请输入共享url', trigger: 'change'}
        ],
        resourcePath: [
          {required: true, message: '请输入资源路径', trigger: 'change'}
        ],
        publisher: [
          {required: true, message: '请输入出版商', trigger: 'change'}
        ],
        share: [
          {required: true, message: '请选择是否共享', trigger: 'change'}
        ],
        content: [
          {required: true, message: '请输入内容', trigger: 'change'}
        ],
        copyright: [
          {required: true, message: '请输入版权', trigger: 'change'}
        ],
        pageIndex: [
          {required: true, message: '请输入页面索引', trigger: 'change'}
        ],
        position: [
          {required: true, message: '请输入位置', trigger: 'change'}
        ],

      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    changeState(data, val) {
      this.$confirm('此操作将修改状态值为 ' + val + ' 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudBook.edit(data).then(res => {
          this.$message({
            type: 'waring',
            message: '修改成功'
          });
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消修改'
        });
      });
    },
    search() {
      getByName(encodeURIComponent(this.input)).then(res => {
        this.crud.data = res;
      })
    }
  }
}
</script>

<style scoped>

</style>
